<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/26 0026
 * Time: 下午 4:34
 */
namespace app\api\controller;

use app\admin\model\Userbuyticket;
use app\common\model\Storecancelrecord;
use app\common\model\Storeuser;
use app\common\model\Userinstore;
use app\common\model\Userinstorecount;
use think\Request;

class Customer extends Base{
    //获取客户列表
    public function getCustomerList(Request $request){
        $param =$request->param();
        $start = $param['start'];
        $length = $param['length'];
        if(empty($param['storeId'])){
            return show(config('status.ERROR_STATUS'),'参数错误','参数错误');
        }
        $join = [
            ['user u','u.id=s.userId']
        ];
        $where['s.storeId'] = $param['storeId'];
        if(!empty($param['keywords'])){
            $keywords = $param['keywords'];
            $where['u.userName'] = ['like','%'.$keywords.'%'];
        }
        $field = 's.*, u.id, u.userName,u.imgUrl';
        $Storeuser = model('storeuser')->alias('s')->where($where)->join($join)->field($field)->limit($start,$length)->select();
        $count = count(model('storeuser')->alias('s')->where($where)->join($join)->field($field)->select());
        if($Storeuser){
            $data['users'] = [];
            $data['totalSize'] = $count;
            foreach ($Storeuser as &$v){
                $Userbuyticket = Userbuyticket::where(['userId'=>$v['id'],'storeId'=>$v['storeId'],'status'=>1])->select();
                $Storecancelrecord = Storecancelrecord::where(['userId'=>$v['id'],'storeId'=>$v['storeId'],'status'=>1])->select();
                $v['userReceiveSize'] = count($Userbuyticket);
                $v['usedTicketSize'] = count($Storecancelrecord);
                array_push($data['users'],$v);
            }

            return show(config('status.SUCCESS_STATUS'),'ok',$data);
        }else{
            return show(2,'暂时没有客户',NULL);
        }
    }

    public function getCustomerDetails(Request $request){
        $param =$request->param();
        if(empty($param['customerId']) || empty($param['storeId'])){
            return show(config('status.ERROR_STATUS'),'参数错误','参数错误');
        }
        $data['user'] = [];
        $User = \app\common\model\User::get($param['customerId']);
        $data['user'] = $User;
        $userInStoresSize = 0;
        $userInStoresWeekSize = 0;
        $userReceiveSize = 0;
        $userReceiveWeekSize = 0;
        $Userinstore = Userinstore::where(['userId'=>$param['customerId'],'storeId'=>$param['storeId']])->find();
        if($Userinstore){
            $userInStoresSize = $Userinstore->inStoreCount;
            $userInStoresTime = $Userinstore->inStoreTime;
        }
        $Userinstorecount = Userinstorecount::where(['userId'=>$param['customerId'],'storeId'=>$param['storeId']])->whereTime('inStoreTime','week')->select();
        if($Userinstorecount){
            foreach ($Userinstorecount as $v){
                $userInStoresWeekSize += (int)$v['inStoreCount'];
            }
        }

        $Userbuyticket = Userbuyticket::where(['userId'=>$param['customerId'],'storeId'=>$param['storeId'],'status'=>1])->order('createDate Desc')->select();
        $UserbuyticketWeek = Userbuyticket::where(['userId'=>$param['customerId'],'storeId'=>$param['storeId'],'status'=>1])->whereTime('createDate','week')->select();
        if($Userbuyticket){
            $userReceiveSize = count($Userbuyticket);
            $userReceiveTime = $Userbuyticket[0]['createDate'];
        }
        if($UserbuyticketWeek){
            $userReceiveWeekSize = count($UserbuyticketWeek);
        }

        $data['userInStoresSize'] = $userInStoresSize;
        $data['userInStoresTime'] = isset($userInStoresTime)?$userInStoresTime:'';
        $data['userInStoresWeekSize'] = $userInStoresWeekSize;
        $data['userReceiveSize'] = $userReceiveSize;
        $data['userReceiveTime'] = isset($userReceiveTime)?$userReceiveTime:'';
        $data['userReceiveWeekSize'] = $userReceiveWeekSize;

        return show(config('status.SUCCESS_STATUS'),'ok',$data);

    }

}